package parser;

import java.util.Hashtable;

import common.AltaRicaModel;

import model.AltaRicaConst;
import model.AltaRicaDomain;
import model.altarica_fields.VarType;

public class AltaRicaParseModel {
	
    private Hashtable<String,AltaRicaParseNode> hash;
    private Hashtable<String,AltaRicaDomain> domains;
    private Hashtable<String,AltaRicaConst> consts;
    
    public AltaRicaParseModel() {
    	
    	this.hash = new Hashtable<String,AltaRicaParseNode>();
    	this.domains = new Hashtable<String,AltaRicaDomain>();
    	this.consts = new Hashtable<String,AltaRicaConst>();
    }
    
    public void addNode(AltaRicaParseNode node) {
    	
    	this.hash.put(node.getName(), node);
    }
    
    public void addDomain(AltaRicaDomain domain) {
    	
    	this.domains.put(domain.getName(),domain);
    }
    
    public Hashtable<String,AltaRicaParseNode> getParsedNodes() {
    	
    	return this.hash;
    }
    
    public Hashtable<String,AltaRicaDomain> getDomains() {
    	
    	return this.domains;
    }
    
    public AltaRicaDomain getDomain(String id) {
    	
    	return this.domains.get(id);
    }
    
    public void addConst(AltaRicaConst cnst) {
		
		this.consts.put(cnst.getId(), cnst);
	}
	
	public AltaRicaConst getConst(String id) {
		
		return this.consts.get(id);
	}
    
	public void compileConsts() {
		
		for (AltaRicaConst cnst : this.consts.values()) {
			
			AltaRicaModel.addConst(cnst);
		}
	}
	
    public void compileDomains() {
    	
    	String str = null;
    	AltaRicaDomain dom = null;
    	VarType type = null;
    	
    	for (AltaRicaDomain domain : getDomains().values()) {
			
    		type = domain.getType();
    		
			if (type.getVarType() == VarType.DOMAIN) {
				
				str = domain.getType().getDomain();
				
				dom = domains.get(str);
				
				if (dom == null) {
					System.out.println("domain not found");
				}
				
				type = dom.getType();
				
				if (type == null) {
					System.out.println("type not found");
				}
				
				domain.setType(type);
			}
			
			AltaRicaModel.addDomain(domain);
		}
    }
    
    public void compileNodes() {
    	
    	for (AltaRicaParseNode parsenode : getParsedNodes().values()) {
			
			if (parsenode.isCompiled() == false)
				parsenode.compile(this);
		}
		
		for (AltaRicaParseNode parsenode : getParsedNodes().values()) {
			AltaRicaModel.addNode(parsenode.getNode());
		}
    }
}
